package com.sinosoft.tcmemr.webservice.dto;

import java.io.Serializable;
import java.util.Map;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import com.sinosoft.tcmemr.webservice.constant.WebServiceConstants;
import com.sinosoft.tcmemr.webservice.util.XmlHandler;

/**
 * WebService服务请求对象
 * @author xiaoxiong
 *
 */
public class Request implements Serializable{

	private static final long serialVersionUID = 1L;

	private Header header;	//	请求头
	
	private Object content;	//	请求内容

	private String xmlStr;	//	xml字符串格式的请求消息
	
	private Document xmlDoc;	//	xml文本格式的请求消息
	
	public Request(String xmlStr) {
		this.xmlStr = xmlStr;
		try {
			this.xmlDoc = DocumentHelper.parseText(this.xmlStr);
		} catch (DocumentException e) {
			e.printStackTrace();
		}
		this.header = (Header) XmlHandler.xmlStrToBean(
				this.getXmlChild(WebServiceConstants.msg_header_node).asXML(), Header.class);
	}
	
	/**
	 * 获取请求参数集合
	 * @return
	 */
	public Map<String, Object> getRequestParams() {
		return XmlHandler.xmlStrToMap(this.xmlStr);
	}
	
	/**
	 * 获取指定的请求参数值
	 * @param param	参数节点名称
	 * @return	参数值
	 */
	public Object getRequsetParamValue(String param) {
		Map<String, Object> params = XmlHandler.xmlStrToMap(this.xmlStr);
		if(params == null) {
			return null;
		} else {
			return params.get(param);
		}
	}
	
	public Header getHeader() {
		return header;
	}

	public void setHeader(Header header) {
		this.header = header;
	}

	public Object getContent() {
		return content;
	}

	public void setContent(Object content) {
		this.content = content;
	}
	
	/**
	 * 获取xml格式消息的子节点
	 * @return	消息子节点
	 */
	private Element getXmlChild(String nodeName) {
        Element root = this.xmlDoc.getRootElement();  
        return root.element(nodeName);
	}
	
}
